<?php

namespace app\admin\controller\mall;

use app\admin\traits\Curd;
use app\common\controller\AdminController;
use app\common\model\MallOrder;
use app\common\model\MallUser;
use app\common\model\MallUserCertification;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
/**
 * @ControllerAnnotation(title="会员管理")
 */
class User extends AdminController
{
    use Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model=(new MallUser());
    }

    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        $type =$this->request->param('type',0,'intval');
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $where[] =[['ishx','=',$type]];
            $count = $this->model
                ->where($where)
                ->count();
            $list = $this->model
                ->where($where)
                ->page($page, $limit)
                ->order($this->sort)
                ->select();
            foreach ($list as $k=>$v){
                $list[$k]['totalpay'] =(new MallOrder())->where([['status','in',[1,2]],['openid','=',$v['openid']]])->sum('payprice');
                $list[$k]['ordercount'] =(new MallOrder())->where([['status','in',[1,2]],['openid','=',$v['openid']]])->count();
            }
            $data = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        // 今日注册用户
        $todaynum =$this->model->where("to_days(FROM_UNIXTIME(create_time)) = to_days(now())")->count();
        $day7num =$this->model->where("DATE_SUB(CURDATE(), INTERVAL 7 DAY) <=date(FROM_UNIXTIME(create_time))  ")->count();
        $allnum =$this->model->count();
        $stopnum =$this->model->where("status=0")->count();
        $this->assign([
            'type'=>$type,
            'todaynum' =>$todaynum,
            'day7num'  =>$day7num,
            'allnum'   =>$allnum,
            'stopnum'  =>$stopnum
        ]);

        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="列表")
     */
    public function getparentcode(){

       $id =$this->request->param('id','0','intval');
       $list = $this->model->field('id,nickName,invite_code')->where("id != '$id'")->select()->toArray();
       $users=[];
       foreach ($list as $k=>$v){
           $users[$k]['id'] =$v['invite_code'];
           $users[$k]['title'] =$v['nickName'];
       }
       $this->success('ok',$users);
    }

}